<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" style="width: 60%" scroll :max-height="700">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      v-loading="formLoading"
    >
      <el-form-item label="所属项目" prop="projectId">
        <el-select v-model="formData.projectId" placeholder="请选择项目" class="w-1/1">
          <el-option
            v-for="simple in SimpleList.list"
            :key="simple.id"
            :label="simple.name"
            :value="simple.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="样本名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入样本名称" />
      </el-form-item>
      <el-form-item label="样本来源" prop="projectId">
        <el-select v-model="formData.resourceId" placeholder="请选择样本来源" class="w-1/1">
          <el-option
            v-for="simple in ResourceSimpleList.list"
            :key="simple.id"
            :label="simple.name"
            :value="simple.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="规格" prop="projectId">
        <el-select v-model="formData.sizeId" placeholder="请选择规格" class="w-1/1">
          <el-option
            v-for="simple in SizeSimpleList.list"
            :key="simple.id"
            :label="simple.name"
            :value="simple.id"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="样本编号" prop="productId">
        <el-input v-model="formData.productId" placeholder="系统自动填充" disabled />
      </el-form-item> -->
      <el-form-item label="样本分类" prop="typeId">
        <el-select v-model="formData.typeId" placeholder="请选择样本分类">
          <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="启用状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择启用状态">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.OPEN_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="样本图片" prop="picture">
        <UploadImg v-model="formData.picture" />
      </el-form-item>
      <el-form-item label="样本描述" prop="detail">
        <el-input
          type="textarea"
          :autosize="{ minRows: 8 }"
          v-model="formData.description"
          placeholder="请输入样本描述"
        />
      </el-form-item>
      <el-form-item label="样本详情" prop="detail">
        <Editor v-model="formData.detail" height="400px" />
      </el-form-item>
      <el-form-item label="序列号" prop="no">
        <el-input v-model="formData.no" placeholder="请输入序列号" />
      </el-form-item>
      <el-form-item label="条形码" prop="code">
        <el-input v-model="formData.code" placeholder="请输入条形码" />
      </el-form-item>
      <el-form-item label="储存器皿" prop="storageVessel">
        <el-input v-model="formData.storageVessel" placeholder="请输入储存器皿" />
      </el-form-item>
      <el-form-item label="课题" prop="researchTopic">
        <el-input v-model="formData.researchTopic" placeholder="请输入课题" />
      </el-form-item>
      <el-form-item label="菌种名称" prop="strainName">
        <el-input v-model="formData.strainName" placeholder="请输入菌种名称" />
      </el-form-item>
      <el-form-item label="病原名称" prop="pathogenName">
        <el-input v-model="formData.pathogenName" placeholder="请输入病原名称" />
      </el-form-item>
      <el-form-item label="是否噬菌" prop="bacteriophagy">
        <el-input v-model="formData.bacteriophagy" placeholder="请输入是否噬菌" />
      </el-form-item>
      <el-form-item label="保藏人" prop="depositor">
        <el-input v-model="formData.depositor" placeholder="请输入保藏人" />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input v-model="formData.realName" placeholder="请输入真实姓名" />
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="formData.age" placeholder="请输入年龄" />
      </el-form-item>
      <el-form-item label="身份证号" prop="idNum">
        <el-input v-model="formData.idNum" placeholder="请输入身份证号" />
      </el-form-item>
      <el-form-item label="采样日期" prop="samplingDate">
        <el-date-picker
          v-model="formData.samplingDate"
          type="date"
          value-format="x"
          placeholder="选择采样日期"
        />
      </el-form-item>
      <el-form-item label="病原级别" prop="pathogenLevel">
        <el-input v-model="formData.pathogenLevel" placeholder="请输入病原级别" />
      </el-form-item>
      <el-form-item label="检测样本类型" prop="sampleType">
        <el-input v-model="formData.sampleType" placeholder="请输入检测样本类型" />
      </el-form-item>
      <el-form-item label="检测结果" prop="testResults">
        <el-input v-model="formData.testResults" placeholder="请输入检测结果" />
      </el-form-item>
      <el-form-item label="采样人" prop="samplingPersonnel">
        <el-input v-model="formData.samplingPersonnel" placeholder="请输入采样人" />
      </el-form-item>
      <el-form-item label="检测人" prop="inspector">
        <el-input v-model="formData.inspector" placeholder="请输入检测人" />
      </el-form-item>
      <el-form-item label="检测日期" prop="testDate">
        <el-date-picker
          v-model="formData.testDate"
          type="date"
          value-format="x"
          placeholder="选择检测日期"
        />
      </el-form-item>
      <el-form-item label="样本数据" prop="fileUrl">
        <UploadFile :is-show-tip="false" v-model="formData.productData" :limit="1" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { JkProductApi, JkProductVO } from '@/api/jikong/jkProduct'
import { JkProductTypeApi, JkProductTypeVO } from '@/api/jikong/jkProductType'
import { JkProjectApi } from '@/api/jikong/project'
import { JkSizeApi, JkSizeVO } from '@/api/jikong/size'
import { JkResourceApi } from '@/api/jikong/resource'

/** 疾控样本表 表单 */
defineOptions({ name: 'JkProductForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const SimpleList = ref([])
const getSimpleList = async () => {
  try {
    SimpleList.value = await JkProjectApi.getJkProjectSimpleList("")
  } finally {
  }
}

const SizeSimpleList = ref([])
const getSizeSimpleList = async () => {
  try {
    SizeSimpleList.value = await JkSizeApi.getJkSizeList("")
  } finally {
  }
}

const ResourceSimpleList = ref([])
const getResourceSimpleList = async () => {
  try {
    ResourceSimpleList.value = await JkResourceApi.getJkResourceList("")
  } finally {
  }
}



const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  productId: undefined,
  name: undefined,
  description: undefined,
  typeId: undefined,
  status: undefined,
  picture: undefined,
  detail: undefined,
  clickCount: undefined,
  projectId: undefined,
  sizeId: undefined,
  areaId: undefined,
  resourceId: undefined,
  no: undefined,
  code: undefined,
  storageVessel: undefined,
  researchTopic: undefined,
  strainName: undefined,
  pathogenName: undefined,
  bacteriophagy: undefined,
  depositor: undefined,
  realName: undefined,
  age: undefined,
  idNum: undefined,
  samplingDate: undefined,
  pathogenLevel: undefined,
  sampleType: undefined,
  testResults: undefined,
  samplingPersonnel: undefined,
  inspector: undefined,
  productData: undefined,
  testDate: undefined
})
const formRules = reactive({
  name: [{ required: true, message: '样本名称不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 疾控分类精简列表 */
const typeList = ref([])
const getTypeList = async () => {
  try {
    const data = await JkProductTypeApi.getJkProductTypeSimpleList()
    typeList.value = data
  } catch (error) {}
}

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  await getSimpleList()
  await getSizeSimpleList()
  await getResourceSimpleList()
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  getTypeList()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await JkProductApi.getJkProduct(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as JkProductVO
    if (formType.value === 'create') {
      await JkProductApi.createJkProduct(data)
      message.success(t('common.createSuccess'))
    } else {
      await JkProductApi.updateJkProduct(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    productId: undefined,
    name: undefined,
    description: undefined,
    typeId: undefined,
    status: undefined,
    picture: undefined,
    detail: undefined,
    clickCount: undefined,
    projectId: undefined,
    sizeId: undefined,
    areaId: undefined,
    resourceId: undefined,
    no: undefined,
    code: undefined,
    storageVessel: undefined,
    researchTopic: undefined,
    strainName: undefined,
    pathogenName: undefined,
    bacteriophagy: undefined,
    depositor: undefined,
    realName: undefined,
    age: undefined,
    idNum: undefined,
    samplingDate: undefined,
    pathogenLevel: undefined,
    sampleType: undefined,
    testResults: undefined,
    samplingPersonnel: undefined,
    inspector: undefined,
    testDate: undefined,
    productData: undefined

  }
  formRef.value?.resetFields()
}
</script>
